<template>
  <div class="app-container">
    <avue-crud
      ref="crud"
      :page="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :permission="permissionList"
      @on-load="getPage"
      :before-open="beforeUpdate"
      @refresh-change="refreshChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDel"
      @sort-change="sortChange"
      @search-change="searchChange"
      @selection-change="selectionChange"
    >
      <template
        slot="videoUrlForm"
        slot-scope="scope"
      >
        <upload-file
          v-model="scope.row.videoUrl"
          :fileType="['.mp4']"
          :limit="1"
        />
      </template>

      <template
        slot="imgUrlForm"
        slot-scope="scope"
      >
        <upload-img
          v-model="scope.row.imgUrl"
          :operate="operate"
        />
      </template>

      <template
        slot="imgUrl"
        slot-scope="scope"
      >
        <img-info v-model="scope.row.imgUrl" />
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { checkPermi } from "@/utils/permission";
import {
  getPage,
  editObj,
} from "@/api/ad/gd";
import { tableOption } from "@/const/crud/ad/gd";

export default {
  name: "GdManage",
  data () {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
        ascs: [],
        descs: "subscribe_time",
      },
      paramsSearch: {},
      tableLoading: false,
      tableOption: tableOption,
      selectionData: [],
      dialogTagging: false,
      checkedTags: [],
      userTagsData: [],
      taggingType: "",
      dialogMsgVisible: false,
      wxUserId: "",
    };
  },
  computed: {
    permissionList () {
      return {
        // addBtn: checkPermi(["wxmp:wxuser:add"]),
        // delBtn: checkPermi(["wxmp:wxuser:del"]),
        // editBtn: checkPermi(["wxmp:wxuser:edit"]),
        // viewBtn: checkPermi(["wxmp:wxuser:get"]),
      };
    },
  },
  methods: {
    beforeUpdate (done, type) {
      this.operate = type;
      console.log(this.operate)
      done();
    },
    toTagging () {
      let openIdList = [];
      for (let i = 0; i < this.selectionData.length; i++) {
        openIdList.push(this.selectionData[i].openId);
      }
      if (this.checkedTags.length <= 0) {
        this.$message.error("请选择标签");
        return;
      }
      if (openIdList.length <= 0) {
        this.$message.error("请选择用户");
        return;
      }
      this.tableLoading = true;
      tagging({
        taggingType: this.taggingType,
        tagIds: this.checkedTags,
        openIds: openIdList,
      })
        .then((response) => {
          this.tableLoading = false;
          this.checkedTags = [];
          this.$refs.crud.selectClear();
          if (response.code == 200) {
            this.getPage(this.page);
          } else {
            this.$message.error("打标签出错：" + response.msg);
          }
        })
        .catch(() => {
          this.tableLoading = false;
        });
      this.dialogTagging = false;
    },
    selectionChange (list) {
      this.selectionData = list;
    },
    searchChange (params, done) {
      params = this.filterForm(params);
      this.paramsSearch = params;
      this.page.currentPage = 1;
      this.getPage(this.page, params);
      done();
    },
    sortChange (val) {
      let prop = val.prop
        ? val.prop.replace(/([A-Z])/g, "_$1").toLowerCase()
        : "";
      if (val.order == "ascending") {
        this.page.descs = [];
        this.page.ascs = prop;
      } else if (val.order == "descending") {
        this.page.ascs = [];
        this.page.descs = prop;
      } else {
        this.page.ascs = [];
        this.page.descs = [];
      }
      this.getPage(this.page);
    },
    getPage (page, params) {
      this.tableLoading = true;
      getPage(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            descs: this.page.descs,
            ascs: this.page.ascs,
            appType: "2",
          },
          params,
          this.paramsSearch
        )
      )
        .then((response) => {
          this.tableData = response.rows;
          this.page.total = response.total;
          this.page.currentPage = page.currentPage;
          this.page.pageSize = page.pageSize;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    updateRemark (row, index) {
      this.$prompt("请输入备注", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "输入不能为空",
      }).then(({ value }) => {
        this.tableLoading = true;
        row.remark = value;
        updateRemark(row)
          .then((response) => {
            this.tableLoading = false;
            if (response.code == 200) {
              this.tableData.splice(index, 1, Object.assign({}, row));
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success",
              });
              this.getPage(this.page);
            } else {
              this.$message.error(response.msg);
            }
          })
          .catch(() => {
            this.tableLoading = false;
          });
      });
    },
    handleDel: function (row, index) {
      var _this = this;
      this.$confirm("是否确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delObj(row.id);
        })
        .then((data) => {
          _this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
          });
          this.getPage(this.page);
        })
        .catch(function (err) { });
    },
    /**
     * @title 数据更新
     * @param row 为当前的数据
     * @param index 为当前更新数据的行数
     * @param done 为表单关闭函数
     *
     **/
    handleUpdate: function (row, index, done, loading) {
      editObj(row)
        .then((data) => {
          this.$message({
            showClose: true,
            message: "修改成功",
            type: "success",
          });
          done();
          this.getPage(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    /**
     * @title 数据添加
     * @param row 为当前的数据
     * @param done 为表单关闭函数
     *
     **/
    handleSave: function (row, done, loading) {
      editObj(row)
        .then((data) => {
          this.$message({
            showClose: true,
            message: "添加成功",
            type: "success",
          });
          done();
          this.getPage(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    /**
     * 刷新回调
     */
    refreshChange (page) {
      this.getPage(this.page);
    },
  },
};
</script>

<style lang="scss" scoped></style>
